package mapEditor
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.net.FileFilter;
	
	import loader.LoaderBase;
	import loader.LoaderEvent;
	import loader.LoaderFactory;
	import loader.LoaderFlagType;
	import loader.LoaderQueue;
	import loader.LoaderType;
	import loader.URLLoaderBase;
	
	import map.findPath.FindPathBuildNode;
	import map.findPath.FindPathNode;
	
	import mapEditor.data.AssetTemplateInfo;
	import mapEditor.data.ConfigTemplateInfo;
	import mapEditor.data.ResTemplateInfo;
	import mapEditor.events.MapEditorEvent;
	import mapEditor.model.MapEditorModel;
	import mapEditor.view.resItemEdit.ResItemEditItemView;
	import mapEditor.vo.AssetVO;
	import mapEditor.vo.MapTerrainTileVO;
	import mapEditor.vo.ResItemItemVO;
	import mapEditor.vo.ResItemVO;
	import mapEditor.vo.ToolType;

	public class MapEditorManager extends EventDispatcher
	{
		public var mapEditorModel:MapEditorModel;
		private var _loaderQueue:LoaderQueue;
		
		public function MapEditorManager()
		{
		}
		
		private static var _instance:MapEditorManager;
		public static function get instance():MapEditorManager
		{
			if(_instance == null)
			{
				_instance = new MapEditorManager();
			}
			return _instance;
		}
		
		public function register():void
		{
			mapEditorModel=new MapEditorModel();
			mapEditorModel.addEventListener(MapEditorEvent.TOOL_TYPE_CHANGE, __onToolTypeChange);
		}
		
		private function __onToolTypeChange(evt:MapEditorEvent):void
		{
			switch(mapEditorModel.toolType)
			{
				case ToolType.OPEN_MAP:
					openMap();
					break;
				case ToolType.SAVE_MAP:
					saveMap();
					break;
			}
		}
		
		/**
		 *保存地图数据 
		 * 
		 */		
		private function saveMap():void
		{
			var findPathBuildNode:FindPathBuildNode=new FindPathBuildNode(mapEditorModel.mapNodes, ConfigTemplateInfo.instance.mapVO.mapRow, ConfigTemplateInfo.instance.mapVO.mapCols, ConfigTemplateInfo.instance.mapVO.walkableHeight, ConfigTemplateInfo.instance.mapVO.walkableBottom);
			mapEditorModel.findPathNodes=findPathBuildNode.buildNode();
		}
		
		/**
		 *打开已有的地图文件 
		 */		
		private function openMap():void
		{
			var file:File = File.applicationDirectory;
			var openFileFilter:FileFilter = new FileFilter("XML", "*.xml");
			file.browseForOpen("请选择要打开的地图文件", [openFileFilter]);
			file.addEventListener(Event.SELECT, __onOpenSelectHandle);
		}
		
		private var _configXml:XML;
		private var _assetXml:XML;
		private var _resXml:XML;
		/**
		 *打开文件选择完成后 
		 */		
		private function __onOpenSelectHandle(evt:Event):void
		{
			var file:File = evt.target as File;
			ConfigTemplateInfo.instance.mapVO.mapFilePath=file.nativePath;
			var fileStream:FileStream=new FileStream();
			fileStream.open(file,FileMode.READ);
			_configXml = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
			fileStream.close();
			fileStream=null;
			file=null;
			
			_loaderQueue=new LoaderQueue();
			_loaderQueue.addEventListener(LoaderEvent.COMPLETE_QUEUE, __onLoaderQueueComplete);
			var load:URLLoaderBase=LoaderFactory.Instance.createLoader(LoaderType.URLLOADER, ConfigTemplateInfo.instance.mapVO.mapFileDir+"\\asset.xml", LoaderFlagType.COMMON, false, false);
			load.addEventListener(LoaderEvent.COMPLETE, __onAssetLibLoadComplete);
			_loaderQueue.addLoader(load);
			load=LoaderFactory.Instance.createLoader(LoaderType.URLLOADER, ConfigTemplateInfo.instance.mapVO.mapFileDir+"\\res.xml", LoaderFlagType.COMMON, false, false);
			load.addEventListener(LoaderEvent.COMPLETE, __onResLibLoadComplete);
			_loaderQueue.addLoader(load);
			_loaderQueue.execute();
		}
		
		private function __onAssetLibLoadComplete(evt:LoaderEvent):void
		{
			evt.baseLoader.removeEventListener(LoaderEvent.COMPLETE, __onAssetLibLoadComplete);
			_assetXml=new XML(evt.baseLoader.data);
			evt.baseLoader.dispose();
			evt.baseLoader=null;
		}
		
		private function __onResLibLoadComplete(evt:LoaderEvent):void
		{
			evt.baseLoader.removeEventListener(LoaderEvent.COMPLETE, __onResLibLoadComplete);
			_resXml=new XML(evt.baseLoader.data);
			evt.baseLoader.dispose();
			evt.baseLoader=null;
		}
		
		private function __onLoaderQueueComplete(evt:LoaderEvent):void
		{
			AssetTemplateInfo.instance.register(_assetXml);
			ResTemplateInfo.instance.register(_resXml);
			ConfigTemplateInfo.instance.removeEventListener(Event.CHANGE, __onMapConfigChange);
			ConfigTemplateInfo.instance.addEventListener(Event.CHANGE, __onMapConfigChange);
			ConfigTemplateInfo.instance.register(_configXml);
		}
		
		private function __onMapConfigChange(evt:Event):void
		{
			dispatchEvent(new MapEditorEvent(MapEditorEvent.OPEN_MAP));
		}
		
		/**
		 *选择一个资源项 
		 * @param assetVO
		 * 
		 */		
		public function selectAssetItem(assetVO:AssetVO):void
		{
			dispatchEvent(new MapEditorEvent(MapEditorEvent.ASSET_ITEM_SELECTED, assetVO));
		}
		
		/**
		 *选择一个单项 
		 * @param resItemVO
		 * 
		 */		
		public function selectResItemItem(resItemVO:ResItemVO):void
		{
			dispatchEvent(new MapEditorEvent(MapEditorEvent.RES_ITEM_ITEM_SELECTED, resItemVO));
		}
		
		/**
		 *选中一个正在编辑中的单项中的资源项 
		 * @param resItemVO
		 * 
		 */
		public function selectResItemItemAsset(resItemItemVO:ResItemItemVO):void
		{
			dispatchEvent(new MapEditorEvent(MapEditorEvent.RES_ITEM_ITEM_ASSET_SELECTED, resItemItemVO));
		}
		
		/**
		 *选中一个地形地块项 
		 * @param resItemVO
		 * 
		 */
		public function selectTerrainTile(mapTerrainTileVO:MapTerrainTileVO):void
		{
			dispatchEvent(new MapEditorEvent(MapEditorEvent.MAP_TERRAIN_TILE_SELECTED, mapTerrainTileVO));
		}
	}
}